package com.bocai.dahuatech.ui.activity;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.bocai.dahuatech.Bean.BaseBean;
import com.bocai.dahuatech.Bean.CityListBean;
import com.bocai.dahuatech.R;
import com.bocai.dahuatech.base.BaseActivity;
import com.bocai.dahuatech.event.MainEvent;
import com.bocai.dahuatech.ui.adapter.CityListAdapter;
import com.bocai.dahuatech.utils.AESUtils;
import com.google.gson.Gson;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 区县选择
 */
public class AreaListActivity extends BaseActivity implements AdapterView.OnItemClickListener{

    @Bind(R.id.lv_city)
    ListView listView;
    CityListBean bean;
    int parentId;
    String province;
    String cityId;
    @Override
    protected int contentView() {
        return R.layout.activity_city_list;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        initToolbar(R.string.citychoose);
        listView.setOnItemClickListener(this);
        parentId=getIntent().getIntExtra("parentId",-1);
        cityId=getIntent().getStringExtra("id");
        province=getIntent().getStringExtra("province");
        getTestCitys(3, parentId);
    }
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        EventBus.getDefault().post(new MainEvent("citychoseover",cityId+"-"+bean.getContent().get(position).getFID(),province+"-"+bean.getContent().get(position).getFItemName()));
        finish();
//        Intent intent=new Intent(this,CityListActivity.class);
//        intent.putExtra("parentId",bean.getContent().get(position).getFID());
//        intent.putExtra("province",province+"-"+bean.getContent().get(position).getFItemName());
//        startActivity(intent);

    }
    private void getTestCitys(int level, int parentId) {
        getTestApi().getPCA(level, parentId)
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    Observer<BaseBean> observer = new Observer<BaseBean>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            hideLoading();
            showToast(R.string.neterror);
            Log.e("error", e.toString());
        }

        @Override
        public void onNext(BaseBean baseBean) {
            if (baseBean.getReturnNo().equals("1")) {
                String result = AESUtils.decode(baseBean.getContent());
                result = start + result + end;
                Log.e("result", result);
                bean=new Gson().fromJson(result,CityListBean.class);
                listView.setAdapter(new CityListAdapter(AreaListActivity.this, bean));
            }
        }

    };
}
